import pandas as pd
from pao_python.pao.data import process_db,dataframe_to_list,DataProcess




class IndustryProduct(DataProcess):
    # def __init__(self, db_addr, db_port,db_name):
    #     DataProcess.__init__(self, db_addr, db_port,db_name)
        # self.db_name = db_name

    #获取行业列表
    def cal_get_industry_list(self):
        res = []
        def process_func(db):
            nonlocal res
            res = self.get_data(db,'IDMIS_Industry',{'parent_id':''})
        process_db(self.db_addr,self.db_port,self.db_name, process_func)
        return res

    def cal_get_product_type_list(self):
        res = []
        def process_func(db):
            nonlocal res
            res = self.get_data(db,'IDMIS_Product_Type',{})
        process_db(self.db_addr,self.db_port,self.db_name, process_func)
        return res
    #获取产品类型/行业字典表数据
    def get_data(self,db,cols_name,data):
        res = {}
        cols = db[cols_name]
        product_list = list(cols.find(data)[:])
        pd_list = pd.DataFrame(product_list).drop(['_id'],axis=1)
        res['result'] = dataframe_to_list(pd_list)
        res['total'] = cols.find(data).count()
        return res
